package com.example.managersystem_01.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.managersystem_01.been.User;
import com.example.managersystem_01.service.impl.UserServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.Map;

@Slf4j
@Controller
public class UserController {

    @Autowired
    UserServiceImpl userService;

    @GetMapping(value = {"/","/login"})
    public String loginController(){
        return "login";
    }

    @GetMapping("/main")
    public String login(
            User user,
            HttpSession session,
            Model model
    ){
        //判断长度是否为0
        if(StringUtils.hasLength(user.getUsername())&&StringUtils.hasLength(user.getPassword())){
            /**
             * 根据密码判断是否注册
             */
            QueryWrapper<User> wrapper=new QueryWrapper<>();
            wrapper.eq("password",user.getPassword());

            Map<String,Object> map=userService.getMap(wrapper);
            Long cont=userService.count(wrapper);

            user.setName((String) map.get("name"));
            //长度不为0则表示有该用户
            if(cont!=0){
                log.info("登录用户:{}",user);
                //保存用户
                session.setAttribute("loginUser",user);
                return "redirect:/main.html";
            }else{
                model.addAttribute("msg","密码错误!");
                return "/login";
            }
        }else{

            //返回登录页
            model.addAttribute("msg","请输入账号或密码");
            return "/login";
        }
    }

    @GetMapping("/main.html")
    public String mainUser(HttpSession session,Model model){
        Object obj=session.getAttribute("loginUser");
        log.info("用户{}",obj);
        model.addAttribute("user",obj);
        if(obj!=null){
            return "main";
        }else{
            model.addAttribute("msg","请重新登录!");
            return "/login";
        }
    }

    /**
     * 注册方法
     */
    @GetMapping("/enrolUser")
    public String enrolUser(){
        return "/sign/enrolUser";
    }
    @GetMapping("/enrolController")
    public String enrolController(
            User user,
            Model model
    ){
        if (StringUtils.hasLength(user.getUsername())&&StringUtils.hasLength(user.getPassword())){

            /**
             * 判断密码是否被人注册
             */
            QueryWrapper<User> wrapper=new QueryWrapper<>();
            wrapper.eq("password",user.getPassword());

            //获取当前密码的数量
            Long cont=userService.count(wrapper);

            //为0 则表示当前密码无人使用
            if (cont==0){
                boolean flag=userService.save(user);
                if (flag){
                    model.addAttribute("msg","注册成功,请登录!");
                }else {
                    model.addAttribute("msg","注册失败!");
                    return "/sign/enrolUser";
                }
            }else{
                model.addAttribute("msg","密码已经被人注册了!");
                return "/sign/enrolUser";
            }
        }else{
            model.addAttribute("msg","请输入账号或密码");
            return "/sign/enrolUser";
        }
        return "login";
    }

    /**
     * 修改页面过渡
     * @return
     */
    @GetMapping("/PwdModify")
    public String getModify(){
        return "/table/ModifyPwd";
    }
    /**
     * 修改密码
     * @param username
     * @param name
     * @param model
     * @return
     */
    @GetMapping("/pwdModify")
    public String pwdModify(
            @RequestParam("username") String username,
            @RequestParam("name") String name,
            Model model
    ){
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("name",name).eq("username",username);

        User user=userService.getOne(queryWrapper);
        log.info("需要修改密码的用户为{}",user);
        if(user!=null){
            model.addAttribute("msg","查询成功!");
        }else{
            model.addAttribute("msg","请先注册!");
        }
        return "/table/ModifyPwd";
    }
}
